package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWmUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private IWmUserClient iWmUserClient;
    @Override
    public PageResponseResult authList(AuthDto authDto) {
        //0.参数判空
        if (authDto==null) {
            return new PageResponseResult(0,0,0);
        }
        //1.入参判断
        authDto.checkParam();
        Integer page = authDto.getPage();
        Integer size = authDto.getSize();
        Short status = authDto.getStatus();
        //2.构建查询条件(审核状态)
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status!=null,ApUserRealname::getStatus,status);
        //3.构建分页构造器
        Page<ApUserRealname> pageInfo = new Page<>(page,size);
        Page<ApUserRealname> pageResult = apUserRealnameMapper.selectPage(pageInfo, wrapper);
        //4.组装并返回
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto authDto, Integer type) {
        //0.参数判空
        if (authDto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.接受参数
        Integer id = authDto.getId();
        String msg = authDto.getMsg();

        //2.获取用户修改状态和原因
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(id);
        if (apUserRealname==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }
        //2.1未通过
        if(type==2){
            apUserRealname.setStatus((short)2);
            apUserRealname.setReason(msg);
            apUserRealnameMapper.updateById(apUserRealname);
        }else if(type==1){
            //2.2审核通过
            apUserRealname.setStatus((short)9);
            apUserRealnameMapper.updateById(apUserRealname);

            ResponseResult responseResult = iWmUserClient.addUser(apUserRealname.getName());
            if(responseResult==null||responseResult.getCode()!=200){
                throw new RuntimeException("自媒体用户创建失败");
            }
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short)1);
            int updateResult = apUserMapper.updateById(apUser);
            if(updateResult<1){
                throw new RuntimeException("App用户状态更新失败");
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
